package com.workpaper;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import com.workpaper.app.tray.AppTrayItem;
import com.workpaper.app.utils.platform.SWTPlatformUtils;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

    public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        super(configurer);
    }
    
    @Override
    public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
        return new ApplicationActionBarAdvisor(configurer);
    }
    
    @Override
    public void preWindowOpen() {
    	//获取配置信息
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setShowCoolBar(true);	//禁用工具栏
        configurer.setShowStatusLine(false);	//禁用状态栏
        configurer.setTitle("Workpaper"); //标题
        
        /*
         * 居中对齐 
         */
        Rectangle rect = Display.getDefault().getBounds();
        int screenWidth = rect.width;	//获取屏幕宽度
        int screenHeight = rect.height;	//获取屏幕高度
        int winWidth = (int)(screenWidth * 0.7);	//窗口宽度： 屏幕80%
        int winHeight = (int)(screenHeight * 0.6);	//窗口高度：屏幕70%
        
        int winX = (screenWidth - winWidth) / 2;	//x轴 坐标
        int winY = (screenHeight - winHeight) / 2;	//y轴 坐标
        configurer.setInitialSize(new Point(winWidth, winHeight));	//设置窗口大小：真正决定了窗口大小
        configurer.getWindow().getShell().setBounds(winX, winY, winWidth, winHeight);	//设置shell的边界，这里的winWidth、winHeight并不起作用
    }

	@Override
	public void postWindowOpen() {
		//创建系统托盘
		AppTrayItem.getInstance().createSystemTrayItem();
	}

	@Override
	public boolean preWindowShellClose() {
		//系统最小化：
		SWTPlatformUtils.getShell().setMinimized(true);
		//窗口不可见，在任务栏不显示应用，做到真关闭
		SWTPlatformUtils.getShell().setVisible(false);
		return false;	//返回false，则不会关闭应用
	}

	@Override
	public void dispose() {
		AppTrayItem.getInstance().dispose();
		super.dispose();
	}
}
